﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class Manage_Emp1_Emp1BaseInfo : AdminPageBase
{
    int id;
    protected void Page_Load(object sender, EventArgs e)
    {
        CheckAdmin();
        GetUserPermisions();
        GetEmpId();
        if (!IsPostBack)
        {
            BindCenter();
            BindGrade();
            BindBrand();
            BindCustomerName();
          //  BindCustomer();
            BindEmp();
            if (add == 1)
            {
                btnsave.Enabled = true;
            }
            else
            {
                btnsave.Enabled = false;
            }

        }
    }
    protected void BindCenter()
    {
        DataSet ds = jtDataAccess.SearchCenter("",10,0,"Name","ASC");
        if (CoreTools.CheckDataSet(ds))
        {
            ddlcenter.DataSource = ds;
            ddlcenter.DataTextField = "Name";
            ddlcenter.DataValueField = "CenterId";
            ddlcenter.DataBind();
        }
        ListItem item = new ListItem("","0");
        ddlcenter.Items.Insert(0,item);
    }
    protected void BindGrade()
    {
        DataSet ds = jtDataAccess.SearchGrade("", 10, 0, "Name", "ASC");
        if (CoreTools.CheckDataSet(ds))
        {
            ddlgrade.DataSource = ds;
            ddlgrade.DataTextField = "Name";
            ddlgrade.DataValueField = "GradeId";
            ddlgrade.DataBind();
        }
        ListItem item = new ListItem("", "0");
        ddlgrade.Items.Insert(0, item);
    }
    protected void BindBrand()
    {
        DataSet ds = jtDataAccess.SearchBrand("", 10, 0, "Name", "ASC");
        if (CoreTools.CheckDataSet(ds))
        {
            ddlbrand.DataSource = ds;
            ddlbrand.DataTextField = "Name";
            ddlbrand.DataValueField = "BrandId";
            ddlbrand.DataBind();
        }
        ListItem item = new ListItem("", "0");
        ddlbrand.Items.Insert(0, item);
    }
    protected void BindCustomerName()
    {
        DataSet ds = jtDataAccess.SearchCustomer("", 10, 0, "Name", "ASC");
        if (CoreTools.CheckDataSet(ds))
        {
            ddlcustomername.DataSource = ds;
            ddlcustomername.DataTextField = "Name";
            ddlcustomername.DataValueField = "CustomerId";
            ddlcustomername.DataBind();
        }
        ListItem item = new ListItem("", "0");
        ddlcustomername.Items.Insert(0, item);
    }
    protected void BindCustomer()
    {
        DataSet ds = jtDataAccess.SearchCustomer("", 10, 0, "Name", "ASC");
        if (CoreTools.CheckDataSet(ds))
        {
           ddlcustomer.DataSource = ds;
           ddlcustomer.DataTextField = "Code";
           ddlcustomer.DataValueField = "CustomerId";
           ddlcustomer.DataBind();
        }
        ListItem item = new ListItem("", "0");
        ddlcustomer.Items.Insert(0, item);
    }
    protected void GetEmpId()
    {
        if (Request.QueryString["id"] == null)
        {
            id = 0;

        }
        else
        {
            id = Convert.ToInt32(Request.QueryString["id"]);
           

        }
    }
    protected void CheckAdmin()
    {
        if (AdminSessionHelper.UserDetails == null)
        {
            JsHelper.TopAlertRedirect("请先登录", "../login.aspx", this.Page);
        }
    }
    protected void BindEmp()
    {
        if (id != 0)
        {
            ddlcenter.Enabled = false;
            ddlcustomer.Enabled = false;
           
            DataSet ds = jtDataAccess.GetEmpInfo(id);
            if (CoreTools.CheckDataSet(ds))
            {
                txtcode.Text = ds.Tables[0].Rows[0]["Code"].ToString();
                txtentrydate.Text = Convert.ToDateTime(ds.Tables[0].Rows[0]["EntryDate"].ToString()).ToString("yyyy-MM-dd").Replace("1900-01-01", "");
                txtleavedate.Text = Convert.ToDateTime(ds.Tables[0].Rows[0]["LeaveDate"].ToString()).ToString("yyyy-MM-dd").Replace("2099-12-31", "").Replace("1900-01-01", "");
                txtName.Text = ds.Tables[0].Rows[0]["Name"].ToString();
                ddlbrand.SelectedValue = ds.Tables[0].Rows[0]["BrandId"].ToString();
                ddlcenter.SelectedValue = ds.Tables[0].Rows[0]["CenterId"].ToString();
                ddlcustomername.SelectedValue = ds.Tables[0].Rows[0]["CustomerId"].ToString();
                ddlcustomer.SelectedValue = ds.Tables[0].Rows[0]["CustomerId"].ToString();
                ddlgrade.SelectedValue = ds.Tables[0].Rows[0]["GradeId"].ToString();
                rbtlFinishClass.SelectedValue = ds.Tables[0].Rows[0]["FinishClass"].ToString();
                rbtlIsTeacher.SelectedValue = ds.Tables[0].Rows[0]["IsTeacher"].ToString();
                int centerid = Convert.ToInt32(ds.Tables[0].Rows[0]["CenterId"].ToString());
                DataSet dsc = jtDataAccess.GetCustomerByCenterId(centerid);
                if (CoreTools.CheckDataSet(dsc))
                {
                    ddlcustomer.DataSource = ds;
                    ddlcustomer.DataTextField = "Code";
                    ddlcustomer.DataValueField = "CustomerId";
                    ddlcustomer.DataBind();
                }

            }
        }
        else
        {
            ddlcenter.Enabled = true;
            ddlcustomer.Enabled = true;
        }
    }
    protected void btnsave_Click(object sender, EventArgs e)
    {
        string name =txtName.Text.Trim();
        string code = txtcode.Text.Trim();
        int  centerid= Convert.ToInt32(ddlcenter.SelectedValue);
        int isteacher = Convert.ToInt32(rbtlIsTeacher.SelectedValue);
        int finishclass = Convert.ToInt32(rbtlFinishClass.SelectedValue);
        int brandid = Convert.ToInt32(ddlbrand.SelectedValue);
        int gradeid = Convert.ToInt32(ddlgrade.SelectedValue);
        int customerid = Convert.ToInt32(ddlcustomer.SelectedValue);
        DateTime entrydate = DateTime.MinValue;
        if(txtentrydate.Text.Trim()!="")
            entrydate = DateTime.Parse(txtentrydate.Text.Trim());
        DateTime leavedate = DateTime.MinValue;
        if(txtleavedate.Text.Trim()!="")
            leavedate = Convert.ToDateTime(txtleavedate.Text.Trim());
        if (!CoreTools.ProcessSqlStr(name, 0))
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert(ConfigHelper.ProcessSqlStrErroMsg), false);
            txtName.Focus();
        }
        else if (!CoreTools.ProcessSqlStr(code, 0))
        {
            ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert(ConfigHelper.ProcessSqlStrErroMsg), false);
            txtcode.Focus();
        }
        else
        {
            JTDataAccess.OpType OpType;
            if (id == 0)
            {
                OpType = JTDataAccess.OpType.Insert;

            }
            else
            {
                OpType = JTDataAccess.OpType.Update;

            }

            int flag = jtDataAccess.OpEmp(OpType, id, name, code, customerid, 1, gradeid,0, 0, 0, brandid,0, entrydate, leavedate, isteacher, finishclass);

            switch (flag)
            {
                case -1:
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("操作失败，该加盟商主管已经存在！"), false);

                    break;
                case 0:
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("操作失败！"), false);
                    break;
                default:
                   // ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", CoreTools.JsAlert("操作成功！"), false);
                  
                    ScriptManager.RegisterStartupScript(up1, this.GetType(), "alert", "<script>alert('操作成功！'); parent.document.getElementById('hidempid').value="+ flag + ";</script>", false);
                    break;
            }
        }
    }
    protected void ddlcustomer_SelectedIndexChanged(object sender, EventArgs e)
    {
        string customerid = ddlcustomer.SelectedValue;
        ddlcustomername.SelectedValue = customerid;
        
    }
    protected void ddlcenter_SelectedIndexChanged(object sender, EventArgs e)
    {
        ddlcustomername.SelectedValue = "0";
        ddlcustomer.Items.Clear();
       
        int centerid = Convert.ToInt32(ddlcenter.SelectedValue);
        DataSet ds = jtDataAccess.GetCustomerByCenterId(centerid);
        if (CoreTools.CheckDataSet(ds))
        {
           
            ddlcustomer.DataSource = ds;
            ddlcustomer.DataTextField = "Code";
            ddlcustomer.DataValueField = "CustomerId";
            ddlcustomer.DataBind();
        }
        ListItem item = new ListItem("", "0");
        ddlcustomer.Items.Insert(0, item);
    }
}